/*******************************************************************************
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.core.syncinfo;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import net.sourceforge.perforce.core.internal.Policy;

/**
  * @version $Revision: 185 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class FolderSyncInfo implements Externalizable
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the depot filename. */
	private String depotFileName;
	/** Indicates that the folder is added. */
	private boolean added;

	// ------------------------------------------------------------------------- Public Constructors

	/** Constructor for FolderSyncInfo. */
	public FolderSyncInfo()
	{
		this(null);
	}

	/** Constructor for FolderSyncInfo.
	  *  
	  * @param depotFileName the depot filename.
	  */
	public FolderSyncInfo(String depotFileName)
	{
		this(depotFileName, false);
	}

	/** Constructor for FolderSyncInfo.
	  *  
	  * @param depotFileName the depot filename.
	  * @param added indicates a new folder.
	  */
	public FolderSyncInfo(String depotFileName, boolean added)
	{
		this.depotFileName = depotFileName;
		this.added = added;
	}


	// ------------------------------------------------------------------------------ Public Methods

	/** Gets the depotFileName.
	  * 
	  * @return Returns a String
	  */
	public String getDepotFileName()
	{
		return depotFileName;
	}

	/** Sets the depotFileName.
	  * 
	  * @param depotFileName The depotFileName to set
	  */
	public void setDepotFileName(String depotFileName)
	{
		Policy.assertNotNull(depotFileName);
		this.depotFileName = depotFileName;
	}
	
	/** Retrieves the name of the folder.
	  *
	  * @return the folder's name. 
	  */
	public String getName()
	{		
		int pos = depotFileName.lastIndexOf('/') + 1;
		return depotFileName.substring(pos); 
	}
	
	/** Checks whether the Folder is added.
	  * 
	  * @return true if folder is new, false otherwise.
	  */
	public boolean isAdded()
	{
		return added;
	}
	
	/** @see java.lang.Object#equals(java.lang.Object) */
	public boolean equals(Object other) 
	{
		if (this == other)
		{
			return true;
		}
		
		
		if (!(other instanceof FolderSyncInfo))
		{
			return false;
		}
		
		FolderSyncInfo otherSyncInfo = (FolderSyncInfo) other;
		return 
			getDepotFileName().equals(otherSyncInfo.getDepotFileName()) &&
			isAdded() == otherSyncInfo.isAdded();
	}
	
	/** @see java.lang.Object#hashCode() */
	public int hashCode() 
	{
		return getDepotFileName().hashCode();
	}

	// ------------------------------------------------------------- Public Methods (Externalizable)
	
	/** @see java.io.Externalizable#readExternal(java.io.ObjectInput) */
	public void readExternal(ObjectInput in) 
		throws IOException 
	{
		depotFileName = in.readUTF();
		added = in.readBoolean();
	}

	/** @see java.io.Externalizable#writeExternal(java.io.ObjectOutput) */
	public void writeExternal(ObjectOutput out) 
		throws IOException 
	{
		out.writeUTF(depotFileName);
		out.writeBoolean(added);
	}
}
